package com.zz.yt.test;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.whf.android.jar.base.latte.BaseActivity;
import com.whf.android.jar.idempotent.LatteCountDown;
import com.whf.android.jar.popup.listener.OnClickDecisionListener;
import com.whf.android.jar.util.skip.AMapStartUtils;
import com.whf.android.jar.util.skip.Navigation;
import com.zz.yt.test.delegate.confusion.ConfusionActivity;
import com.zz.yt.test.delegate.net.ANetActivity;
import com.zz.yt.test.delegate.news.NewsActivity;
import com.zz.yt.test.delegate.pdf.DelegateActivity;
import com.zz.yt.test.gloading.FontSizeActivity;

/**
 * @author qf
 * @version 2023-04-23
 */
public class MainActivity extends BaseActivity implements AdapterView.OnItemClickListener {


    private final String[] title = {
            "导航软件选择导航工具类", "倒计时",
            "基础信息界面", "网络相关界面",
            "webView服务相关界面",
            "仿微信字体大小调整", "查看混淆是否影响储存等"
    };

    private final Class[] classes = {
            NewsActivity.class, NewsActivity.class,
            NewsActivity.class, ANetActivity.class,
            DelegateActivity.class,
            FontSizeActivity.class,
            ConfusionActivity.class,
    };

    ListView idListView;

    @Override
    protected Object setLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState) {
        addMarginTopEqualStatusBarHeight(findViewById(R.id.id_title));
        setTextView(R.id.id_title, StringUtils.getString(R.string.app_name));

        initBattery();

        initView();

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        // 处理通过URL Scheme启动的Intent
        // 设置当前 Activity 的 Intent 为新的 Intent
        setIntent(intent);
        if (Intent.ACTION_VIEW.equals(intent.getAction())) {
            Uri uri = intent.getData();
            // 检查URI的scheme和host是否与你的定义匹配
            if (uri != null && "latte".equals(uri.getScheme()) && "core".equals(uri.getHost())) {
                // 提取token，这取决于你在URL中如何放置token
                String token = uri.getQueryParameter("token"); // 假设token是作为查询参数传递的
                // 如果token存在，处理它
                if (token != null) {
                    // 使用token执行相关操作
                    handleToken(token);
                }

            }
        }
    }

    /**
     * 处理传入的 token 参数
     *
     * @param token 传入的 token 参数
     */
    private void handleToken(String token) {
        // 根据 token 执行相应逻辑
        System.out.println("token: " + token);
        ToastUtils.showShort("token = " + token);
    }

    private void initView() {
        idListView = findViewById(R.id.id_list_view);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                MainActivity.this,
                android.R.layout.simple_list_item_1,
                title);
        idListView.setAdapter(adapter);
        idListView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position == 0) {
            AMapStartUtils.init(new Navigation());
            return;
        }
        if (position == 1) {
            LatteCountDown.init(160000, new OnClickDecisionListener() {
                @Override
                public void onConfirm(Object decision) {
                    ToastUtils.showShort(decision + "");
                }

                @Override
                public void onCancel() {

                }
            });
            return;
        }
        ActivityUtils.startActivity(classes[position]);
    }

}

